 
 import { reactive } from "vue";
 
interface RegisterUser {
    name: string;
    email: string;
    password: string;
    password2: string;
    role: string;
}
 export const registerUser = reactive<RegisterUser>({
   name: '',
   email: '',
   password: '',
   password2: '',
   role: ''
 });

 const validatePass2 = (rule: RegisterRules, value: string, callback: any) => {
    if (value === '') {
      callback(new Error('请再次输入密码'));
    } else if (value !== registerUser.password) {
      callback(new Error('两次输入密码不一致!'));
    } else {
      callback();
    }
  };

 interface RegisterRules {
    name: ({
        message: string;
        required: boolean;
        trigger: string;
        min?: undefined;
        max?: undefined;
    } | {
        min: number;
        max: number;
        message: string;
        trigger: string;
        required?: undefined;
    })[];
    email: {
        type: string;
        message: string;
        required: boolean;
        trigger: string;
    }[];
    password: ({
        required: boolean;
        message: string;
        trigger: string;
        min?: undefined;
        max?: undefined;
    } | {
        min: number;
        max: number;
        message: string;
        required?: undefined;
        trigger?: undefined;
    })[];
    password2: ({
        required: boolean;
        message: string;
        trigger: string;
        min?: undefined;
        max?: undefined;
        validator?: undefined;
    } | {
        min: number;
        max: number;
        message: string;
        required?: undefined;
        trigger?: undefined;
        validator?: undefined;
    } | {
        validator: (rule: RegisterRules, value: string, callback: any) => void;
        message?: string;
        trigger?: string;
    })[];
 }

 export const registerRules = reactive<RegisterRules>({
    name: [
     { 
       message: '用户名不能为空...', 
       required: true, 
       trigger: 'blur', 
     },
     {
       min: 2,
       max: 30,
       message: '长度在 2 到 30 个字符之间',
       trigger: 'blur'
     }
   ],
   email: [
       {
         type: 'email', 
         message: 'UserName is incorrect...', 
         required: true, 
         trigger: 'blur', 
       }
     ],
     password: [
        { 
          required: true,
          message: 'Password could not be empty...', 
          trigger: 'blur', 
        },
        {
          min: 6,
          max: 30,
          message: 'Password\'s length has to be 6 to 30 characters...'
        }
      ],
     password2: [
        { 
          required: true,
          message: 'Password2 could not be empty...', 
          trigger: 'blur', 
        },
        {
          min: 6,
          max: 30,
          message: 'Password2\'s length has to be 6 to 30 characters...'
        },
        {
          validator: validatePass2,
          message: '两次输入密码不一致',
        }
      ],
})